<?php
namespace Home\Controller;

use Common\Controller\UserBaseController;

/**
 * 充值、提现控制器
 */
class PaymentController extends UserBaseController
{
    public function __construct()
    {
        parent::__construct();

        $info = M('member')->field('member_name,member_money,member_f_money,last_login_time,true_name,bank_id,bank_num,bank_name,bank_user')->where('member_id=' . $_SESSION['member_id'])->find();
        $this->assign('info', $info);
    }
    /**
     *  充值/提款
     */
    public function deposit()
    {
        if ($_POST) {

            $amount = $_POST['amount'];
            //查看是否有未审核充值
            $cash_id = M('cash_m')->where('m_id=' . $_SESSION['member_id'] . ' and type=1 and status=0')->getField('id');
            if ($cash_id > 0) {
                $data['status']  = 0;
                $data['message'] = '您有充值未处理，请联系管理员';
                $this->ajaxReturn($data, 'json');exit;

            }
            /*   $img = $_POST['screenshot'];
            $img=explode(';base64,',$img);
            $img=$img[1];
            $img = str_replace(' ', '+', $img);

            $image = base64_decode(str_replace(" ","+",$img));

            $name=rand(1000,9999).'_c_'.time().rand(100,999).'.';

            // 上传文件
            $file=date('Ym',time());
            if(!is_dir('upload/payment/'.$_SESSION['member_id']))
            {
            mkdir('data/upload/payment/'.$_SESSION['member_id'].'/');

            }
            $userdir = 'data/upload/payment/'.$_SESSION['member_id'].'/'.$name.'png';
            if($img!=''){
            $imgsss=file_put_contents("$userdir",$image);
            }

            //    $info   =   $upload->upload($image);
            if($imgsss<=0) {// 上传错误提示错误信息
            $data['status']=0;
            $data['message']=$upload->getError();
            $data['message']='44';
            $this->ajaxReturn($data,'json');exit;
            //$this->error($upload->getError(),'deposit');
            }else{// 上传成功
            $data['filename']='/data/upload/payment/'.$_SESSION['member_id'].'/'.$name.'png';
            $data['money']=$amount;
            $data['add_time']=time();
            $data['m_id']=$_SESSION['member_id'];
            $data['type']=1;
            M('cash_m')->add($data);
            $data['status']=1;
            $data['message']='上传成功！,等待后台处理';
            $this->ajaxReturn($data,'json');exit;
            //$this->success('上传成功！','#/payment/transfer');
            } */

            $data['money']    = $amount;
            $data['add_time'] = time();
            $data['m_id']     = $_SESSION['member_id'];
            $data['type']     = 1;
            $data['remark']   = $_POST['remark'];
            M('cash_m')->add($data);
            $data['status']  = 1;
            $data['message'] = '上传成功！,等待后台处理';
            $this->ajaxReturn($data, 'json');exit;
            exit;
        }
        //充值账号
        $payment = M('payment')->order('sort desc')->select();
        $this->assign('payment', $payment);
        $this->display('member-center-deposit');
    }
    //提款
    public function withdrawal()
    {
        if ($_POST) {
            //查询是否有提款未处理
            $id = M('cash_m')->where('m_id=' . $_SESSION['member_id'] . ' and status=0 and type=2')->getField('id');
            if ($id > 0) {
                die(json_encode(array("success" => 0, "message" => '您有提款未处理，请联系管理员')));
            }
            $bank              = M('member')->field('bank_num,bank_user,bank_name,bank_id')->where('member_id=' . $_SESSION['member_id'])->find();
            $data['bank_info'] = json_encode($bank);
            $data['m_id']      = $_SESSION['member_id'];
            $data['money']     = $_POST['drawamount'];
            $data['add_time']  = time();
            $data['type']      = $_POST['type'];
            $data['remark']    = $_POST['remark'];

            $data['bank_name'] = M('bank_list')->where('id=' . $_POST['bankCode'])->getField('name');
            $result            = M('cash_m')->where('member_id=' . $_SESSION['member_id'])->add($data);
            if ($result >= 0) {
                //冻结金额

                M('member')->where('member_id=' . $_SESSION['member_id'])->setDec('member_money', $data['money']); // 用户的余额减少;
                M('member')->where('member_id=' . $_SESSION['member_id'])->setInc('member_f_money', $data['money']); // 用户的冻结金额增加;
                die(json_encode(array("success" => 1, "message" => '银行卡绑定成功')));
            } else {
                die(json_encode(array("success" => 0, "message" => '银行卡绑定失败')));
            }
        }
        $list = M('bank_list')->where('isShow=1')->order('sort asc')->select();
        $this->assign('list', $list);
        $this->display('member-center-withdrawal');
    }
    //绑定
    public function cardsave()
    {
        if ($_POST) {
            $data['bank_address'] = $_POST['bankaddress'];
            $data['bank_num']     = $_POST['cardId'];
            $data['bank_id']      = $_POST['bankCode'];
            $data['bank_user']    = $info['bank_user'];
            $data['bank_name']    = M('bank_list')->where('id=' . $_POST['bankCode'])->getField('name');
            M('member')->where('member_id=' . $_SESSION['member_id'])->save($data);
            if ($result >= 0) {

                die(json_encode(array("success" => 1, "message" => '银行卡绑定成功')));
            } else {
                die(json_encode(array("success" => 0, "message" => '银行卡绑定失败')));
            }

        }
    }
    //解除绑定
    public function card_remove()
    {
        if ($_POST) {
            $data['bank_address'] = '';
            $data['bank_num']     = '';
            $data['bank_id']      = '';
            $data['bank_name']    = '';
            M('member')->where('member_id=' . $_SESSION['member_id'])->save($data);
            if ($result >= 0) {

                die(json_encode(array("success" => 1, "message" => '银行卡解除成功')));
            } else {
                die(json_encode(array("success" => 0, "message" => '银行卡解除失败')));
            }

        }
    }
    //交易记录
    public function transfer()
    {
        $type          = $_GET['transtype'];
        $status        = $_GET['accept'];
        $begin         = $_GET['begin'];
        $end           = $_GET['end'];
        $keyword       = $_GET['keyword'];
        $where['m_id'] = $_SESSION['member_id'];
        if ($begin != '' || $end != '') {
            $where['add_time'] = array('between', array(strtotime($begin), strtotime($end) + 86400));
        }
        if ($type >= 1) {
            $where['type'] = $type;
        }
        if ($status == '-1' || $status == '') {

        } else {
            $where['status'] = $status;
        }

        $count = M('cash_m')->where($where)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('cash_m')->where($where)->order('add_time desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $k => $v) {
            if ($vo['type'] == 1) {

            } else {
                $bank             = json_decode($v['bank_info'], true);
                $money            = $v['money'] - $v['poundage'];
                $list[$k]['desc'] = '账户：' . substr($bank['bank_num'], 0, 4) . '***' . substr($bank['bank_num'], -4) . '&nbsp存入&nbsp;' . $money;

            }
        }
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('type', $type);
        $this->assign('status', $status);
        $this->assign('begin', $begin);
        $this->assign('end', $end);
        $this->display('member-center-transfer');
    }
    //交易详情
    public function detailtrans()
    {
        $id   = $_GET['id'];
        $info = M('cash_m')->where('m_id=' . $_SESSION['member_id'] . ' and id=' . $id)->find();
        $bank = json_decode($info['bank_info'], true);

        $this->assign('bank', $bank);
        $this->assign('info', $info);
        $this->display();
    }
    //取消交易
    public function cancelTrans()
    {
        $id     = $_POST['transId'];
        $result = M('cash_m')->where('m_id=' . $_SESSION['member_id'] . ' and type=1 and status=0')->delete();
        if ($result >= 0) {
            die(json_encode(array("success" => 1, "message" => '取消成功啦！，是否前往订单列表？')));
        } else {
            die(json_encode(array("success" => 0, "message" => '提交失败，请联系客服！')));
        }
    }
}
